﻿/*
CheckboxValueModule is a liteGrid plug-in that adds the ability to have checkbox values

Author: JGK
DateAuthored: 12/9/09  
*/

function CheckboxValueModule() {

    var base = this;

    // function to check if a particular tdElement is editable
    var isEditableCallback;

    //Registers for events.
    base.initialize = function(liteGrid, options) {
        base.liteGrid = liteGrid;
        base.options = options;
        isEditableCallback = (options.isEditableCallback) || function() { return true; };

        liteGrid.$el.bind("columnBound", base.columnBound);
    }

    //Attaches a checkbox to editable columns.
    base.columnBound = function(event) {
        var column = event.column;
        var tdElement = event.element;
        var dataItem = event.dataItem;

        // if it's not a checkbox don't bind thie column
        if (column.type != "checkbox") {
            return;
        }

        //If the column isn't editable, or if we've already applied 
        if (column.editable !== true || !isEditableCallback(column, tdElement, dataItem) || tdElement.data("editable") === true) {
            return;
        }

        var input = $('<input type="checkbox"/>');
        if (dataItem[column.field])
            input.attr('checked', "checked");
        tdElement.html(input);
        input.click(
            function(eventObject) {
                base.saveCell($(this).attr('checked'), event);
            });
    }

    //Callback that is run whenenever a cell has been saved.  This
    //stores the cell value in the underlying data item.
    base.saveCell = function(value, settings) {

        var cell = $(settings.element);

        //See if the value actually changed
        var dataItem = cell.parent().data("dataItem");

        var currentValue = dataItem[settings.column.field];

        //An event is raised so that interested parties can modify the
        //value prior to attempting to persist it.  
        //TODO: ADD HOOKS FOR VALIDATION!
        var event = $.Event("valueChanged");
        event.currentValue = currentValue;
        event.newValue = value;
        event.column = settings.column;

        base.liteGrid.$el.trigger(event);

        //Subscribers may have modified the new value
        value = event.newValue;

        // this should be true unless it was changed by the subscriber aboves
        settings.valueChanged = currentValue != value;

        //If the value has changed, Mark the cell as having been changed.
        if (settings.valueChanged) {
            // toggle the modified state
            if (cell.hasClass("modified")) {
                cell.removeClass("modified")
                    .removeClass("ui-state-highlight");
            }
            else {
                cell.addClass("modified")
                    .addClass("ui-state-highlight");
            }
            dataItem[settings.column.field] = value;
            cell.parent().data("dataItem", dataItem);
        }

        return value;
    }
}